package de.unibremen.mum.app.whatscool;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.Toast;

public class RatingActivity extends Activity {

	private DatabaseHandler db;
	private String url;
	private static final SimpleDateFormat df = new SimpleDateFormat("dd.MM.yyyy");


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_rating);

		db = new DatabaseHandler(this);
		
		url = getIntent().getExtras().getString(MainActivity.INTENT_EXTRA_URL);
		createProductPic(url);
		final RatingBar r1 = (RatingBar) findViewById(R.id.ratingBar1);
		final RatingBar r2 = (RatingBar) findViewById(R.id.ratingBar2);
		final RatingBar r3 = (RatingBar) findViewById(R.id.ratingBar3);
		final RatingBar r4 = (RatingBar) findViewById(R.id.ratingBar4);
		final RatingBar r5 = (RatingBar) findViewById(R.id.ratingBar5);
		final RatingBar r6 = (RatingBar) findViewById(R.id.ratingBar6);
		final EditText productName = (EditText) findViewById(R.id.editProductTitle);

		// Handler beim Klicken auf den Button
		final Button button = (Button) findViewById(R.id.saveBTN);
		button.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				RatingClass rating = new RatingClass(productName.getText().toString(), url, "Kommentar zum Foto", ""
						+ r1.getRating(), "" + r2.getRating(), "" + r3.getRating(), "" + r4.getRating(), ""
						+ r5.getRating(), "" + r6.getRating(), "Wird nicht benotigt", df.format(new Date()));
				//Ergebnis berechnen und zum Rating hinzufügen
				rating.setResult("" + calculateAverage(rating));
				
				long id = db.getID(url);
				//Wenn es noch keinen Eintrag für die URL gibt, soll ein neuer Eintrag angelegt werden
				if(id == -1){
					id = db.addRating(rating);
				}
				else{
					//ansonsten wird der vorhandene mit den neuen Werten aktualisert
					db.updateRating(rating, id);
				}

				Intent myIntent = new Intent(RatingActivity.this, ResultActivity.class);
				myIntent.putExtra("id", id);
				startActivity(myIntent);

			}
		});
	}


	/**
	 * Zeigt das Foto im View an
	 * @param url URL zum Foto
	 */
	public void createProductPic(String url) {
		if ((new File(url)).exists()) {
			Bitmap bitmap = BitmapFactory.decodeFile(url);
			if (bitmap != null) {
				ImageView iv = (ImageView) findViewById(R.id.productPic);
				iv.setImageBitmap(bitmap);
			} else {
				Toast.makeText(RatingActivity.this, "Foto nicht gefunden", Toast.LENGTH_SHORT).show();
			}
		}
	}
	
	/**
	 * Berechnet den Mittelwert der einzelnen Bewertungen
	 * @param rating Rating mit den vorhandenen Bewertungen für die Fragen
	 * @return Mittelwert
	 */
	public double calculateAverage(RatingClass rating) {

		double average = (Double.parseDouble(rating.getQuest1()) + Double.parseDouble(rating.getQuest1())
				+ Double.parseDouble(rating.getQuest1()) + Double.parseDouble(rating.getQuest1())
				+ Double.parseDouble(rating.getQuest1()) + Double.parseDouble(rating.getQuest1())) / 6;
		db.close();
		return average;
	}

}
